package com.xingkeduo.business.user.controller;

import com.xingkeduo.business.transformation.json.*;
import com.xingkeduo.business.user.domain.CraftsmanDomain;
import com.xingkeduo.business.user.domain.FeignDomain;
import com.xingkeduo.business.user.domain.ManageInfoDomain;
import com.xingkeduo.business.user.domain.PnpLogDomain;
import com.xingkeduo.pojo.dto.*;
import com.xingkeduo.pojo.query.*;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.pojo.utils.ResponseUtils;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.ReturnUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一线员工
 *
 * @author 高杰
 * @time 2019年1月7日 下午6:57:47
 * @description
 */
//@CrossOrigin
@RestController
@RequestMapping("/craftsman")
public class CraftsmanController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(CraftsmanController.class);

    @Resource
    private ManageInfoDomain manageInfoDomain;
    @Resource
    private FeignDomain feignDomain;
    @Resource
    private CraftsmanDomain craftsmanDomain;
    @Resource
    private PnpLogDomain pnpLogDomain;

    /**
     * 获取配置的服务类型信息 pantong  2019年5月18日上午11:32:05
     *
     * @param id
     * @return String
     */
    @GetMapping(value = "/getUserServiceConfig/{id}")
    public String getUserServiceConfig(@PathVariable("id") Long id) {
        returnUtil = new ReturnUtil();
        try {
            if (id == null || id.longValue() < 0) {
                throw new InputParamException(InputParamException.Type.INVALID, "id" + " error");
            }
            CraftsmanServiceQO craftsmanServiceQO = new CraftsmanServiceQO();
            craftsmanServiceQO.setCraftsmanId(id);
            return returnUtil.getSuccess(CraftsmanJTO.toJsonOfUserServiceConfig(manageInfoDomain.getUserServiceConfig(craftsmanServiceQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 获取配置的店铺信息 pantong  2019年5月18日上午11:32:05
     *
     * @param id
     * @return String
     */
    @GetMapping(value = "/getCraftsmanShops/{id}")
    public String getCraftsmanShops(@PathVariable("id") Long id) {
        returnUtil = new ReturnUtil();
        try {
            if (id == null || id.longValue() < 0) {
                throw new InputParamException(InputParamException.Type.INVALID, "id" + " error");
            }

            CraftsmanShopQO craftsmanShopQO = new CraftsmanShopQO();
            craftsmanShopQO.setCraftsmanId(id);
            return returnUtil.getSuccess(CraftsmanJTO.toJsonOfUserShops(manageInfoDomain.getCraftsmanShops(craftsmanShopQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 手动新增发型师
     *
     * @param body:
     * @param request:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/8/16 17:50
     */
    @PostMapping("/insert/{body}")
    public String insertCraftsman(@PathVariable String body, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanTO craftsman = CraftsmanJTO.createTableForModify(body);
            CraftsmanJobTO craftsmanJob = CraftsmanJobJTO.createTableForModify(body);
            CraftsmanSocialTO craftsmanSocial = CraftsmanSocialJTO.createTableForModify(body);
            CraftsmanDutyQO craftsmanDutyQO = CraftsmanDutyJTO.createQueryById(body);
            CraftsmanLevelQO craftsmanLevelQO = CraftsmanLevelJTO.createQueryForPrimary(body);
            craftsmanDomain.cmsInsertCraftsmanInfo(craftsman, craftsmanJob, craftsmanSocial, craftsmanDutyQO, craftsmanLevelQO, getOperator(request));
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 详情接口
     *
     * @return
     * @author 高杰
     * @time 2019年1月7日 下午6:57:57
     */
    @GetMapping(value = "/{url:.+}")
    public String doGet(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQuery = CraftsmanJTO.createQueryForPrimary(url);
            return returnUtil.getSuccess(CraftsmanJTO.toJson(manageInfoDomain.getCraftsmanWithDetail(craftsmanQuery)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 修改接口
     *
     * @return
     * @author 高杰
     * @time 2019年1月9日 下午3:31:25
     */
    @RequestMapping(value = "/{url:.+}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String url, @RequestBody String body, ServerHttpRequest request) {
        log.info("发型师编辑");
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQuery = CraftsmanJTO.createQueryForPrimary(url);
            CraftsmanTO craftsman = CraftsmanJTO.createTableForModify(body).setId(craftsmanQuery.getId());
            CraftsmanJobTO craftsmanJob = CraftsmanJobJTO.createTableForModify(body).setId(craftsmanQuery.getId());
            CraftsmanSocialTO craftsmanSocial = CraftsmanSocialJTO.createTableForModify(body)
                    .setId(craftsmanQuery.getId());
            BaseDTO companyDTO = BaseDTOJTO.createQueryForPrimary(body);
            CraftsmanShopQO shopQO = CraftsmanShopJTO.createQueryForShopId(body);
            manageInfoDomain.modifyCraftsmanDetail(craftsman, craftsmanSocial, craftsmanJob, companyDTO, shopQO, getOperator(request));
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 外部接口,访问根据code查询发型师info
     *
     * @param code:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/11 19:47
     */
    @GetMapping("/code/{code}")
    public String getCraftsmanInfoForCode(@PathVariable String code) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQuery = CraftsmanJTO.createQueryForList(code);
            log.info("craftsmanQo:--------------" + craftsmanQuery.toString());
            return returnUtil.getSuccess(CraftsmanSocialJTO.toJson(manageInfoDomain.getCraftsmanNameForCode(craftsmanQuery)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 外部接口服务接口  根据id 获取发型师
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/7 20:28
     */
    @GetMapping("/get/{id}")
    public ResponseUtils<UserDTO> getCraftsmanInfo(@PathVariable String id) {
        try {
            CraftsmanQO craftsmanQuery = CraftsmanJTO.createQueryForPrimary(id);
            log.info("craftsmanQo:--------------" + craftsmanQuery.toString());
            UserDTO craftsmanForId = manageInfoDomain.getCraftsmanForId(craftsmanQuery);
            return ResponseUtils.getSuccess(craftsmanForId);
        } catch (Throwable t) {
            dealException(t);
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

    /**
     * 外部接口 (订单) 根据发型师id,昵称,手机号 查询发型师list
     *
     * @param :
     * @return : com.xingkeduo.pojo.utils.ResponseUtils<com.xingkeduo.pojo.dto.WxMaUserInfo>
     * @author : gaozifeng
     * @date : 2019/7/5 11:49
     */
    @GetMapping("/list/condition/{condition}")
    public ResponseUtils<List<Long>> getUserIdsByCondition(@PathVariable String condition) {
        try {
            CraftsmanQO queryForList = CraftsmanJTO.createQueryForCondition(condition);
            CraftsmanJobQO jobQO = CraftsmanJobJTO.createQueryForConditionByCode(condition);
            CraftsmanSocialQO socialQO = CraftsmanSocialJTO.createQueryForConditionByCode(condition);
            return ResponseUtils.getSuccess(manageInfoDomain.getCraftsmanIdByCondition(queryForList, jobQO, socialQO));
        } catch (Throwable throwable) {
            return ResponseUtils.error(throwable.getMessage(), "-1");
        }
    }

    /**
     * 应该调用祥达接口,不应该在这里封装(门店列表)
     * 现在只有后台有用到
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/5 14:01
     */
    @GetMapping("/shop/list")
    public String getShopList() {
        returnUtil = new ReturnUtil();
        try {
            List<BaseDTO> shopForList = feignDomain.getShopForList("{}");
            return returnUtil.getSuccess(BaseDTOJTO.toJson(shopForList));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 外部接口，内部可用，发型师id，名称，艺名
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/13 20:19
     */
    @GetMapping("/condition/{condition}")
    public ResponseUtils<BaseDTO> getCraftsman(@PathVariable String condition) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForStatuss(condition);
            CraftsmanJobQO craftsmanJobQO = CraftsmanJobJTO.createQueryForCondition(condition);
            CraftsmanSocialQO craftsmanSocialQO = CraftsmanSocialJTO.createQueryForList(condition);
            List<BaseDTO> craftsmanInfo = manageInfoDomain.getCraftsmanInfo(craftsmanQO, craftsmanJobQO, craftsmanSocialQO);
            return ResponseUtils.getSuccess(craftsmanInfo);
        } catch (Throwable t) {
            dealException(t);
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

    //app接口

    /**
     * 登录，修改密码,等 发短信
     *
     * @param param:
     * @param :      2 更换手机号 不做手机号验证
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/9 16:55
     */
    @PostMapping("/send/sms")
    public String sendSms(@RequestBody String param) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanSocialQO queryForPhone = CraftsmanSocialJTO.createQueryForPhone(param);
            craftsmanDomain.sendSms(queryForPhone);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询登录注册状态
     *
     * @param id:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/8 16:48
     */
    @GetMapping("/register/{id}")
    public String checkRegister(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            return returnUtil.getSuccess(CraftsmanJTO.toJson(craftsmanDomain.checkRegister(craftsmanQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询手机号是否存在发型师表
     *
     * @param phone:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/9 11:19
     */
    @GetMapping("/checkphone")
    public String checkPhone(String phone) {
        returnUtil = new ReturnUtil();
        try {
            craftsmanDomain.checkUserPhone(phone);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 获取发型师信息
     *
     * @param url:id值
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/19 13:52
     */
    @GetMapping(value = "/info/{url:.+}")
    public String getCraftsmanDetail(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(url);
            return returnUtil.getSuccess(CraftsmanJTO.toJson(craftsmanDomain.getCraftsmanSocial(craftsmanQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询经理城市列表
     *
     * @param id:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/6/3 17:15
     */
    @GetMapping("/shopids/{id}")
    public String getCraftsmanShopIdForList(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            return returnUtil.getSuccess(BaseDTOJTO.toJson(craftsmanDomain.getShopIdForList(craftsmanQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 注册     完善  修改发型师信息;
     *
     * @param params:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/19 14:17
     */
    @PutMapping(value = "/edit/register/{url:.+}")
    public String modifyCranftsmanSocial(@PathVariable String url, @RequestBody String params) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO queryForCheckPwd = CraftsmanJTO.createQueryForCheckPwd(params).setId(CraftsmanJTO.createQueryForPrimary(url).getId());
            CraftsmanJobTO jobTO = CraftsmanJobJTO.createTableForModify(params);
            CraftsmanTO cto = new CraftsmanTO().setPassword(queryForCheckPwd.getPassword()).setId(queryForCheckPwd.getId()).setRegisterStep(queryForCheckPwd.getRegisterStep());
            cto.setFiles(CraftsmanFileJTO.createTableForModify(params, cto.getId()));
            cto.setJob(jobTO);
            CraftsmanSocialTO socialTO = CraftsmanSocialJTO.createTableForModify(params);
            cto.setSocial(socialTO);
            craftsmanDomain.modifyCraftsmanSocial(cto);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询发型师是否有健康证
     *
     * @param url:员工id
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/20 15:45
     */
    @GetMapping("/health/{url}")
    public String ckeckHealth(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanHealthQO queryForPrimary = CraftsmanHealthJTO.createQueryForList(url);
            return returnUtil.getSuccess(CraftsmanHealthJTO.toJson(craftsmanDomain.ckeckHealth(queryForPrimary.setIsDelete(YesNoEnum.NO))));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 上传健康证信息
     *
     * @param craftsmanId:
     * @param json:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/20 19:51
     */
    @PostMapping("/health/insert/{craftsmanId}")
    public String insertHealth(@PathVariable String craftsmanId, @RequestBody String json) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanHealthTO healthTO = CraftsmanHealthJTO.createTableForModify(json).setCraftsman_id(CraftsmanHealthJTO.createQueryForList(craftsmanId).getCraftsman_id());
            healthTO.setFileList(CraftsmanFileJTO.createTableForModify(json, healthTO.getCraftsman_id()));
            craftsmanDomain.insertHealth(healthTO);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 更换绑定手机号
     *
     * @param id:
     * @param phone: 包含 code和phone
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/29 16:20
     */
    @PutMapping("/change/phone/{id}")
    public String binding(@PathVariable String id, @RequestBody String phone) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            CraftsmanSocialQO craftsmanSocialQO = CraftsmanSocialJTO.createQueryForPhone(phone);
            craftsmanDomain.changeCraftsmanPhone(craftsmanQO, craftsmanSocialQO);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 更改绑定微信账号
     *
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/29 17:25
     */
    @PostMapping("/change/openid/{id}")
    public String changeOpenid(@PathVariable String id, @RequestBody SubscribeDTO subscribeDTO) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            craftsmanDomain.changeCraftsmanOpenid(craftsmanQO, subscribeDTO);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 修改发型师个人信息 app
     *
     * @param id:
     * @param json:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/16 11:41
     */
    @PostMapping("/edit/{id}")
    public String editCraftsmanInfo(@PathVariable String id, @RequestBody String json) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanTO cto = new CraftsmanTO().setId(CraftsmanJTO.createQueryForPrimary(id).getId());
            CraftsmanSocialTO tableForModify = CraftsmanSocialJTO.createTableForModify(json);
            cto.setSocial(tableForModify);
            cto.setJob(CraftsmanJobJTO.createTableForModify(json));
            CraftsmanHealthTO healthTO = CraftsmanHealthJTO.createTableForModify(json).setCraftsman_id(cto.getId());
            cto.setFiles(CraftsmanFileJTO.createTableForModify(json, cto.getId()));
            craftsmanDomain.editCraftsmanInfo(cto, healthTO);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 登出  传token 解析并查询Cratfsman
     *
     * @param token:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/30 10:24
     */
    @PostMapping("/out/{token}")
    public String out(@PathVariable String token) {
        returnUtil = new ReturnUtil();
        //TODO 待完善
        return returnUtil.getSuccess();

    }

    /**
     * 发型师联系用户
     *
     * @param pnpLogDTO:
     * @return : com.xingkeduo.pojo.utils.ResponseUtils<java.lang.String>
     * @author : gaozifeng
     * @date : 2019/6/20 14:26
     */
    @PostMapping("/requestCallConsumer")
    public ResponseUtils<String> requestCallConsumer(@RequestBody PnpLogDTO pnpLogDTO) {
        try {
            String virtualPhone = pnpLogDomain.requestCallConsumer(pnpLogDTO);
            return ResponseUtils.getSuccess(virtualPhone, "1");
        } catch (Throwable t) {
            dealException(t);
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

    /**
     * 外部接口--获取发型师信息
     *
     * @param url:id值
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/19 13:52
     */
    @GetMapping(value = "/getinfo/{url:.+}")
    public ResponseUtils<CraftsmanDTO> getCraftsmanDetailInfo(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(url);
            CraftsmanDTO craftsmanDTO = craftsmanDomain.getCraftsmanSocialInfo(craftsmanQO);
            return ResponseUtils.getSuccess(craftsmanDTO);
        } catch (Throwable t) {
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

}
